package com.liyi.letter.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liyi.letter.common.config.MD5;
import com.liyi.letter.common.exception.LetterException;
import com.liyi.letter.common.result.ResultCodeEnum;
import com.liyi.letter.system.entity.Department;
import com.liyi.letter.system.entity.Employee;
import com.liyi.letter.system.entity.EmployeeRole;
import com.liyi.letter.system.mapper.EmployeeMapper;
import com.liyi.letter.system.service.DepartmentService;
import com.liyi.letter.system.service.EmployeeRoleService;
import com.liyi.letter.system.service.EmployeeService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @author Administrator
 * @description 针对表【employee】的数据库操作Service实现
 * @createDate 2023-06-06 10:13:35
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
        implements EmployeeService {
    @Autowired
    private EmployeeRoleService employeeRoleService;
    @Autowired
    private DepartmentService departmentService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addEmployee(Employee employee) {
        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper.eq("username", employee.getUsername());
        Employee employee1 = baseMapper.selectOne(employeeQueryWrapper);
        if (StringUtils.isEmpty(employee1)) {
            System.err.println("employee.getPhoto() = " + employee.getPhoto());
            employee.setPassword(MD5.encrypt(employee.getPassword()));
            baseMapper.insert(employee);
            employee.getRoles().forEach(role -> {
                EmployeeRole employeeRole = new EmployeeRole();
                employeeRole.setId(null);
                employeeRole.setEmployeeId(employee.getId());
                employeeRole.setRoleName(role.getName());
                employeeRole.setRoleId(role.getId());
                employeeRoleService.save(employeeRole);
            });
        } else {
            throw new LetterException(ResultCodeEnum.USERNAME_ERROR);
        }
        return true;
    }

    @Override
    public void updateEmployee(Employee employee) {
        baseMapper.updateById(employee);
        QueryWrapper<EmployeeRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employee_id", employee.getId());
        employeeRoleService.remove(queryWrapper);
        employee.getRoles().forEach(role -> {
            EmployeeRole employeeRole = new EmployeeRole();
            employeeRole.setId(null);
            employeeRole.setEmployeeId(employee.getId());
            employeeRole.setRoleName(role.getName());
            employeeRole.setRoleId(role.getId());
            employeeRoleService.save(employeeRole);
        });
    }

    @Override
    public void deletedEmployeeById(Long id) {
        baseMapper.deleteById(id);
        QueryWrapper<EmployeeRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employee_id", id);
        employeeRoleService.remove(queryWrapper);
    }

    @Override
    public Page MyPage(Integer page, Integer pageSize, Employee employee) {

        Page<Employee> iPage = new Page(page, pageSize);

        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(employee!=null) {
        lambdaQueryWrapper.like(employee.getName() != null, Employee::getName, employee.getName());
        lambdaQueryWrapper.eq(employee.getAge() != null, Employee::getAge, employee.getAge());
        lambdaQueryWrapper.eq(employee.getGender() != null, Employee::getGender, employee.getGender());
        lambdaQueryWrapper.eq(employee.getPhone() != null, Employee::getPhone, employee.getPhone());
        lambdaQueryWrapper.orderByDesc(Employee::getUpdatedTime);
        }
        baseMapper.selectPage(iPage, lambdaQueryWrapper);

        iPage.getRecords().forEach(record->{
            Long departmentId = record.getDepartmentId();
            if (departmentId!=null) {
                Department departmentServiceById = departmentService.getById(departmentId);
                if (departmentServiceById!=null) {
                    String name = departmentServiceById.getName();
                record.setDepartmentName(name);
            }}
        });
        return iPage;
    }
}




